package com.zzyl.nursing.controller.member;

import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 预约信息Controller
 *
 * @author ruoyi
 */
@Slf4j
@RestController
@RequestMapping("/member/reservation")
@Api(tags = "预约信息相关接口")
public class MemberReservationController extends BaseController {

    @Autowired
    private IReservationService reservationService;

    /**
     * 分页查询预约
     */
    @GetMapping("/page")
    @ApiOperation(value = "获取预约信息列表")
    public R<TableDataInfo<Reservation>> list(Integer pageNum, Integer pageSize, Integer status) {
        log.info("分页查询预约信息");
        List<Reservation> list = reservationService.selectReservationList(pageNum, pageSize, status);
        return R.ok(getDataTable(list));
    }


    /**
     * 获取预约信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:reservation:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取预约信息详细信息")
    public R<Reservation> getInfo(@ApiParam(value = "预约信息ID", required = true)
                                  @PathVariable("id") Long id) {
        return R.ok(reservationService.selectReservationById(id));
    }

    /**
     * 新增预约信息
     */
    @PostMapping
    @ApiOperation("新增预约信息")
    public AjaxResult add(@ApiParam(value = "预约信息实体", required = true) @RequestBody Reservation reservation) {
        return toAjax(reservationService.insertReservation(reservation));
    }

    /**
     * 修改预约信息
     */
    @PutMapping("{id}/cancel")
    @ApiOperation("修改预约信息")
    public AjaxResult edit(@ApiParam(value = "预约信息实体", required = true)@PathVariable Long id) {
        return toAjax(reservationService.updateReservation(id));
    }

    /**
     * 删除预约信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:reservation:remove')")
    @Log(title = "预约信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除预约信息")
    public AjaxResult remove(@ApiParam(value = "要删除的数据id的数组") @PathVariable Long[] ids) {
        return toAjax(reservationService.deleteReservationByIds(ids));
    }

    /**
     * 查询取消预约数量
     *
     * @return
     */
    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public R<Integer> getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        System.out.println("------" + userId);
        int count = reservationService.getCancelledReservationCount(userId);
        return R.ok(count);
    }

    /**
     * 查询每个时间段剩余的预约数量   TODO
     *
     * @param time
     * @return
     */
    @GetMapping("/countByTime")
    @ApiOperation("查询预约数量")
    public R<List<Map>> getReservationCountByTime(long time) {
        Long userId = UserThreadLocal.getUserId();
        System.out.println("------" + userId);
        List<Map> maps = reservationService.getReservationCountByTime(userId, time);
        return R.ok(maps);
    }


}